#!/bin/bash
# 完整测试结果存储路径
RESULT_FILE="./result.txt"
# 报告文件存储路径
REPORT_FILE="./report.csv"
# 临时文件路径
TEMP_FILE="./temp.txt"
# 用于消除标准输出
OUTPUT="2>/dev/null"

# 4K随机读写测试
CMD_TEST_FILEIO_4K="sysbench fileio --file-num=10 --threads=8 --file-total-size=1G --file-block-size=4K --file-test-mode=rndrw"
# CPU测试
CMD_TEST_CPU="sysbench cpu --cpu-max-prime=2000 run"
# 线程测试命令
CMD_TEST_THREAD="sysbench threads --threads=500 --thread-yields=100 --thread-locks=4 run"
# 内存测试命令
CMD_TEST_MEMORY_8K="sysbench memory --memory-block-size=8k --memory-total-size=1G run"
CMD_TEST_MEMORY_4M="sysbench memory --memory-block-size=4M --memory-total-size=1G run"

FILEIO_PREPARE="prepare"
FILEIO_RUN="run"
FILEIO_CLEANUP="cleanup"

# 清空上次的测试记录
rm -rf $RESULT_FILE
rm -rf $REPORT_FILE
rm -rf $TEMP_FILE

echo "======================Test Start=======================" >> $RESULT_FILE
# CPU测试
echo "======================CPU Test=========================" >> $RESULT_FILE
echo $CMD_TEST_CPU >> $RESULT_FILE
$CMD_TEST_CPU | tee -a $RESULT_FILE | tee $TEMP_FILE

#echo "CPU测试结果" >> $REPORT_FILE
#echo "执行次数/s" >> $REPORT_FILE
CPU_RESULT_LINE=`cat $TEMP_FILE | grep second: | awk -F: '{print $2;}' | awk '{print $1}' | tr '\n' ', ' | sed 's/,$//'`
echo "CPU:"$CPU_RESULT_LINE >> $REPORT_FILE

#echo "" >> $REPORT_FILE
#echo "" >> $REPORT_FILE

# 线程测试
echo "======================Thread Test=======================" >> $RESULT_FILE
echo $CMD_TEST_THREAD >> $RESULT_FILE
$CMD_TEST_THREAD | tee -a $RESULT_FILE | tee $TEMP_FILE

#echo "线程测试结果" >> $REPORT_FILE
#echo "执行事件数" >> $REPORT_FILE
THREAD_RESULT_LINE=`cat $TEMP_FILE | grep events: | awk -F: '{print $2;}' | awk '{print $1}' | tr '\n' ', ' | sed 's/,$//'`
echo "Threads:"$THREAD_RESULT_LINE >> $REPORT_FILE

#echo "" >> $REPORT_FILE
#echo "" >> $REPORT_FILE

# 内存测试
echo "======================Memory Test=======================" >> $RESULT_FILE
echo $CMD_TEST_MEMORY_8K >> $RESULT_FILE
$CMD_TEST_MEMORY_8K | tee -a $RESULT_FILE | tee $TEMP_FILE
$CMD_TEST_MEMORY_4M | tee -a $RESULT_FILE | tee -a $TEMP_FILE

#echo "内存测试结果" >> $REPORT_FILE
#echo "传输速度MB/s(8KB),传输速度MB/s(4MB)" >> $REPORT_FILE
MEMORY_RESULT_LINE=`cat $TEMP_FILE | grep transferred | awk -F\( '{print $2;}' | awk '{print $1;}' | tr '\n' ', ' | sed 's/,$//'`
echo "Memory:"${MEMORY_RESULT_LINE} >> $REPORT_FILE

#echo "" >> $REPORT_FILE
#echo "" >> $REPORT_FILE

# 磁盘IO测试
echo "======================FileIO Test======================" >> $RESULT_FILE
echo $CMD_TEST_FILEIO_4K >> $RESULT_FILE
$CMD_TEST_FILEIO_4K $FILEIO_PREPARE
$CMD_TEST_FILEIO_4K $FILEIO_RUN | tee -a $RESULT_FILE | tee $TEMP_FILE
$CMD_TEST_FILEIO_4K $FILEIO_CLEANUP

#echo "硬盘IO测试结果" >> $REPORT_FILE
#echo "读操作/s,写操作/s,fsyncs同步/s,读吞吐率（MB）,写吞吐率（MB）" >> $REPORT_FILE
DISK_RESULT_LINE=`cat $TEMP_FILE | grep /s: | awk -F: '{print $2;}' | awk '{print $1}' | tr '\n' ', ' | sed 's/,$//'`
echo "Disk:"${DISK_RESULT_LINE} >> $REPORT_FILE

echo "" >> $REPORT_FILE




